package com.example.cmscrud.demo.controller;

import com.example.cmscrud.demo.entity.Users;
import com.example.cmscrud.demo.service.impl.UsersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UsersController {
    @Autowired
    private UsersServiceImpl usersService;
    /*
    get请求获取用户列表
     */
    @RequestMapping("/users")
    @ResponseBody
    public List<Users> getUser(){
        return  usersService.getAllUser(1,10);
    }
    /*
    post请求添加用户
     */
    @RequestMapping(value = "/postuser",method = RequestMethod.POST)
    @ResponseBody
    public int insertUser(Users users){
        System.out.println(users);
        String md5Password=DigestUtils.md5DigestAsHex(users.getPassword().getBytes());
        System.out.println(md5Password);
        users.setPassword(md5Password);
        return usersService.insertUser(users);
    }
    /*
    delete请求删除用户数据
     */
    @RequestMapping(value = "/deleteuser",method = RequestMethod.DELETE)
    public int deleteUser(int id){
        System.out.println(id);
        return usersService.deleteUser(id);
    }

    /*
    update请求修改用户数据
     */
    @RequestMapping(value = "/updateuser",method = RequestMethod.POST)
    public int updateUser(Users users){
        return usersService.updateUser(users);
    }
}
